const { Cart } = require('../../../model/cart');
const { decodeToken } = require('../../../middleGuard/jwt.js')

module.exports = async (req, res) => {
    const userId = decodeToken(req.headers.authorization).userId
    const { goodsId, businessId } = req.body
    const data = await Cart.findOne({user: userId, goods: goodsId})
    if(!data) {
      Cart.create({ user: userId, business: businessId, goods: goodsId }).then(item => {
        res.json({
            data: item,
            message: '添加成功！'
        })
      }).catch((err) => {
        res.status(403)
        res.json({
            log: err,
            message: '添加失败！'
        })
      })
    } else {
      res.status(403)
      res.json({
          message: '商品已在购物车！'
      })
    }
}